package com.redoor.app.stake.workflow.mq.consumerService;

import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.redoor.app.stake.basicdata.model.BaseMqOperationLog;
import com.redoor.app.stake.basicdata.model.BaseStake;
import com.redoor.app.stake.basicdata.model.BussinessTypeEnum;
import com.redoor.app.stake.basicdata.model.LogEnum;
import com.redoor.app.stake.basicdata.mq.sendService.SendMQService;
import com.redoor.app.stake.basicdata.service.BaseStakeService;
import com.redoor.app.stake.basicdata.service.LogService;
import com.redoor.app.stake.common.utils.UUIDTool;
import com.redoor.app.stake.workflow.service.WorkOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class FesOperation implements MessageListener {

    private static final Logger logger = LoggerFactory.getLogger(FesOperation.class) ;

    @Autowired
    public BaseStakeService baseStakeService;

    @Autowired
    public WorkOrderService workorderService;

    @Autowired
    LogService logService;

    @Override
    public Action consume(Message message, ConsumeContext context) {
        String result = "" ;
        String tag = "";

        try {
            result = new String(message.getBody());
            tag = message.getTag();
        }catch (Exception e){
            e.printStackTrace();
        }

        logger.info("FesOperation-->mq body："+result+"  ---->mq tag："+tag);

        if (StringUtils.isEmpty(result)){
            logger.info("FesOperation接收到的mq body为空! tag="+tag);
        }else {
            if ("completeDebug_tag".equals(tag)){
                BaseMqOperationLog baseMqOperationLog = new BaseMqOperationLog();
                baseMqOperationLog.setUuid(UUIDTool.getUUID());
                baseMqOperationLog.setMessagetopic(message.getTopic());
                baseMqOperationLog.setMessagebody(result);
                baseMqOperationLog.setMessagetag(tag);
                baseMqOperationLog.setType(2);
                afterDebugData(result,baseMqOperationLog);
            }
        }

        return Action.CommitMessage;
    }

    private void afterDebugData(String mqbody,BaseMqOperationLog baseMqOperationLog){
        String[] results = mqbody.toString().split(",") ;
        String stakeNo = results[0] ;
        if (StringUtils.isEmpty(stakeNo)){
            logger.info("stakeNo from FesOprationMq is null ");
        }

        BaseStake stake = baseStakeService.getStakeByStakeNo(stakeNo);
        if (stake!=null){
            stake.setDebuggingtime(new Date());
            stake.setIsdebugging(1);
            baseStakeService.updateStakeBySelective(stake, BussinessTypeEnum.TRACE_TYPE.MQ,baseMqOperationLog.getUuid());

            /*****begin 功能描述:生成桩维护工单接口调用  author:zhangMumu  2018/4/1 0001 8:18*****/
            logService.addLogToRedis(baseMqOperationLog, LogEnum.MQ_LOG);
            //这里仅仅将这个工单的方法改成了调用接口,没有修改逻辑
            workorderService.maApplySave(stake,baseMqOperationLog.getUuid());
            /*****end 功能描述:生成桩维护工单接口调用  author:zhangMumu  2018/4/1 0001 8:18*****/

        }else {
            logger.warn("创建工单失败，根据桩编号" + stakeNo + "未查到对应的桩信息!"); ;
        }

    }

}